package vg.modules.conveyor.components;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.util.Collection;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import vg.services.data_base_manager.data.graph.Attribute;
import vg.services.data_base_manager.data.graph.Vertex;
import vg.services.data_base_manager.realization.DataBaseTypeParser;
import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.main_manager.MainManager;
import vg.services.plugin_manager.event_and_request.event.graph_editor.AddAttributeEvent;

public class ChangeVertexParametersDialog {
	// Swing components
	private static JComponent[] dialogComponents = null;
	private static JLabel executorsLabel;
	private static JComboBox executorsComboBox;
	private static JLabel nameLabel;
	private static JTextField nameTextField;
	
	// Mutex
	private static final Object generalMutex = new Object();
	
	public static void show(final Collection<String> executors, final Vertex vertex, final AGraphView graphView) {
		synchronized (generalMutex) {
			createComponents4Dialog(executors, vertex);
			if (JOptionPane.showConfirmDialog(MainManager.view.getMainFrame(), dialogComponents, "Conveyor settings", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE) == 0) {
				Object selectedItem = executorsComboBox.getSelectedItem();
				String executor = selectedItem == null ? "" : selectedItem.toString();
				String name = nameTextField.getText();
				
				int nameAttrId = MainManager.model.createAttributeHeader4Vertex(vertex.getStorableId(), "name", name, DataBaseTypeParser.string2class(DataBaseTypeParser.STRING));
				int executorAttrId = MainManager.model.createAttributeHeader4Vertex(vertex.getStorableId(), "executor", executor, DataBaseTypeParser.string2class(DataBaseTypeParser.STRING));
				
				final Attribute nameAttr = MainManager.model.getAttribute(nameAttrId);
				final Attribute executorAttr = MainManager.model.getAttribute(executorAttrId);
				
				Thread t = new Thread(new Runnable() {
					@Override
					public void run() {
						if (graphView != null) {
							graphView.getLowLevelGraphEditor().addAttribute4Vertex(nameAttr, vertex);
							graphView.getLowLevelGraphEditor().addAttribute4Vertex(executorAttr, vertex);
						}
					}
				});
				t.setDaemon(true);
				t.start();
				
				AddAttributeEvent event = new AddAttributeEvent(nameAttr);
				MainManager.view.addEvent(event);
			}
		}
	}
	
//=============================================================================
//-----------------PRIVATE METHODS---------------------------------------------
	private static void createComponents4Dialog(final Collection<String> executors, final Vertex vertex) {
		synchronized (generalMutex) {
			if (!SwingUtilities.isEventDispatchThread()) {
				try {
					SwingUtilities.invokeAndWait(new Runnable() {
						@Override
						public void run() {
							synchronized (generalMutex) {
								createComponents4Dialog(executors, vertex);									
							}
						}
					});
				} catch (Exception ex) {
					MainManager.log.printException(ex);
				}
			} else {
				nameLabel = new JLabel("Name:");
				nameTextField = new JTextField("1");
				
				executorsLabel = new JLabel("Executors:");
				if (executors == null || executors.size() == 0) {
					executorsComboBox = new JComboBox();
				} else {
					String[] objs = new String[executors.size()];
					objs = executors.toArray(objs);
					executorsComboBox = new JComboBox(objs);
				}
				
				dialogComponents = new JComponent[4];
				dialogComponents[0] = nameLabel;
				dialogComponents[1] = nameTextField;
				dialogComponents[2] = executorsLabel;
				dialogComponents[3] = executorsComboBox;
			}
		}
	}
}
